--#include "data\config\boss\BossHomeConfig.lua" once
--[[BOSS的刷出、复活机制剥离出来，在CommonBoss.lua中实现
本文件只处理BOSS之家独特的功能
]]


--[[
注册一些事件 for test
]]
function ResisterBossHomeProc()
	--print("ResisterBossHomeProc...")
	for homeIdx, homeCfg in ipairs( BossHomeConfig ) do
		--SceneEnterDispatcher.register( homeCfg.sceneId, OnEnterBossHome )
		SceneExitDispatcher.register( homeCfg.sceneId, OnExitBossHome )				
	end

	--	该副本是需要掉线恢复的
	SetNeedReloginFuben( BossHomeFubenConfig.fubenId, GlobalLuaConfig.reloginFuben.bossHomeFuben, GetElemsForReLoginBossHomeFuben )
	SceneExitDispatcher.register( BossHomeFubenConfig.sceneId, OnCommonSceneExit )					--注册离开场景、副本后触发的事件
	SceneHandExitDispatcher.register( BossHomeFubenConfig.sceneId, OnCommonSceneHandExit )			--注册手工离开场景、副本后触发的事件
	SceneLogoutDispatcher.register( BossHomeFubenConfig.sceneId, OnCommonFubenLogout )				--注册场景、副本中下线触发的事件
end



-------------------------------------------BOSS之家副本----------------------------------------------
function EnterBossHomeFuben( sysarg )
	if CommonEnterScence( sysarg, BossHomeFubenConfig, GameLog.clBossHomeEnterFee, Lang.ScriptTips.BossHomeLog01 ) then
		InitBossHomeFubenPanel( sysarg )
	end
end

--[[
初始化右侧面板
]]
function InitBossHomeFubenPanel( sysarg )
	if not IsInBossHomeFuben( sysarg ) then
		return
	end
	local fubenPtr = Actor.getFubenPrt(sysarg)

	local activityType 	= 1 			--活动
	local activityId 	= enRightPanelActivity_BossHome
	local restTime 		= Fuben.getFbTime( fubenPtr )
	if restTime < 0 then
		restTime = 0
	end

	local panelInfo 	= {}
	panelInfo.sts 		= 1  										--进行中
	panelInfo.title 	= Lang.ScriptTips.BossHomeName
	panelInfo.timeTitle = Lang.ScriptTips.panelCont01				--剩余时间
	panelInfo.restTime	= restTime


	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end

function IsInBossHomeFuben( sysarg )
	local fubenId = Actor.getFubenId( sysarg )
	return fubenId == BossHomeFubenConfig.fubenId
end

--[[
掉线恢复的一些配置
]]
function GetElemsForReLoginBossHomeFuben( fubenPtr )
	local fubenCfg 				= BossHomeFubenConfig
	local InitFubenRightPanel 	= InitBossHomeFubenPanel
	local loginLog   			= "bosshomne FB login" 
	local loginLogOk			= "bosshomne FB login ok" 
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end


-------------------------------------------BOSS之家场景----------------------------------------------
--[[
进入BOSS之家的某一层
oldhomeIdx：0，表示从外部进入
newhomeIdx：要进入的层
]]
function EnterBossHome( sysarg, oldhomeIdx, newhomeIdx )
	--print("EnterBossHome, oldhomeIdx="..oldhomeIdx..", newhomeIdx="..newhomeIdx)
	--[[
	local bossHomeCfg = GetBossHomeCfgBySceneId( Actor.getSceneId( sysarg ) )
	if bossHomeCfg then 			--已经在场景中
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0032, ttFlyTip)
		return
	end
	]]
	local bossHomeCfg = BossHomeConfig[newhomeIdx]
	if not bossHomeCfg then
		return
	end

	local bossHomeCfgIn = GetBossHomeIdleHome( bossHomeCfg )		--选择是否需要进入空闲场景
	if not bossHomeCfgIn then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.BossHome007, ttFlyTip)
		return
	end

	local bossHomeCfgInCopy = DeepCopyFromObject( bossHomeCfgIn )		--深拷贝
	if oldhomeIdx > newhomeIdx then					--从高层进入低层，不需要扣费
		bossHomeCfgInCopy.enterConsume = nil 		--返回不扣费 
	end
	--print("EnterBossHome, bossHomeCfgIn="..bossHomeCfgInCopy.sceneId)
	if CommonEnterScence( sysarg, bossHomeCfgInCopy, GameLog.clBossHomeEnterFee, Lang.ScriptTips.BossHomeLog01 ) then
		--if oldhomeIdx == 0 and newhomeIdx == 1 then 				--从外部进入进入第1层
		local dDvar = Actor.getDyanmicVar(sysarg)
		dDvar.enterBossHomeTime = System.getCurrMiniTime()
		--end
		InitBossHomePanel( sysarg )

		--注册回城的延迟消息
		Actor.removeEntityScriptDelayMsg( sysarg, 17 )
		--[[最后的true表示实体死亡，消息保留]]
		Actor.postEntityScriptDelayMsg( sysarg, bossHomeCfg.beInTime*1000, 0, 17,0,0,0,0,false,true )

		--有时可能活动面板没有打开成功，几秒后再打开一次
		Actor.removeEntityScriptDelayMsg( sysarg, DELAY_MSG_ACTIVITY_PANEL )
		Actor.postEntityScriptDelayMsg( sysarg, 5*1000, 0, DELAY_MSG_ACTIVITY_PANEL,0,0,0,0,false,true )

		System.broadcastTipmsgLimitLev( string.format(Lang.ScriptTips.BossHome005, Actor.getName( sysarg )), ttChatWindow, bossHomeCfg.enterLevelLimit[2])
	end
end

--for test
function OnEnterBossHome( sysarg, sceneId, lastSceneId )
	print("OnEnterBossHome, sceneId="..sceneId..", lastSceneId="..lastSceneId)
end

function GetBossHomeIdleHome( bossHomeCfg )
	if not bossHomeCfg.extHomes then		--没有扩展层
		return bossHomeCfg
	end

	local playerList = LuaHelp.getSceneActorListById( bossHomeCfg.sceneId ) or {}
	--print("GetMayaPalaceIdlePlace, #playerList="..#playerList)
	if #playerList < bossHomeCfg.playerNum then
		return bossHomeCfg
	end

	for i, extHomeIdx in ipairs( bossHomeCfg.extHomes ) do
		local extHomeCfg = GetBossHomeCfgByIdx( extHomeIdx )		--找到1个隐藏层
		local playerList = LuaHelp.getSceneActorListById( extHomeCfg.sceneId ) or {}
		--print("GetBossHomeIdleHome, extHome, sceneId="..extHomeCfg.sceneId..", #playerList="..#playerList..", playerNum="..extHomeCfg.playerNum)
		if extHomeCfg.playerNum and #playerList < extHomeCfg.playerNum then
			return extHomeCfg
		end
	end

	local extHomeIdx = bossHomeCfg.extHomes[#bossHomeCfg.extHomes]
	local extHomeCfg = GetBossHomeCfgByIdx( extHomeIdx )
	return extHomeCfg
end

--[[获取配置]]
function GetBossHomeCfgByIdx( homeIdx )
	--print("GetBossHomeCfgByIdx, homeIdx="..homeIdx)
	for idx, homeCfg in ipairs( BossHomeConfig ) do
		if homeCfg.extHomeIdx == homeIdx then
			return homeCfg
		end
	end
	return nil
end

--[[
楼层变化
可能是从外面进入，可能是内部楼层切换，可能是退出活动
]]
function OnBossHomeChange( sysarg, nowSceneId, lastSceneId )
	--print("OnBossHomeChange, nowSceneId="..nowSceneId..", lastSceneId="..lastSceneId)
	local nowSceneIsBossHome 	= IsInBossHome( nowSceneId )
	local lastSceneIsBossHome 	= IsInBossHome( lastSceneId )
	if lastSceneIsBossHome and not nowSceneIsBossHome then 		--原场景在内，新场景在外，就是退出BOSS之家
		local fubenId = Actor.getFubenId( sysarg )
		OnExitBossHome( sysarg, lastSceneId, fubenId ) 		--离开BOSS之家
	end

end

--[[
退出BOSS之家
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnExitBossHome( sysarg, sceneId, fubenId )
	--print("OnExitBossHome, sceneId="..sceneId)
	CloseCountDownPanel(sysarg)
	CloseActivityRightPanel(sysarg)							--关闭面板
	Actor.removeEntityScriptDelayMsg( sysarg, 17 )			--离开BOSS之家场景，删除延迟消息
	Actor.removeEntityScriptDelayMsg( sysarg, DELAY_MSG_ACTIVITY_PANEL )
end


--[[
返回下一层
]]
function PrivBossHome( sysarg )
	--print("PrivBossHome...")
	local bossHomeCfg = GetBossHomeCfgBySceneId( Actor.getSceneId( sysarg ) )
	if not bossHomeCfg then 			--不在场景中，请先进入
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.BossHome002, ttFlyTip)
		return
	end

	local privHomeIdx 		= bossHomeCfg.homeIdx - 1
	local privBossHomeCfg 	= BossHomeConfig[privHomeIdx]
	if not privBossHomeCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.BossHome006, ttFlyTip)
		return
	end

	local  oldhomeIdx 	= bossHomeCfg.homeIdx
	local  newhomeIdx 	= privHomeIdx
	EnterBossHome( sysarg, oldhomeIdx, newhomeIdx )
end

--[[
进入下一层
]]
function NextBossHome( sysarg )
	--print("NextBossHome...")
	local bossHomeCfg = GetBossHomeCfgBySceneId( Actor.getSceneId( sysarg ) )
	if not bossHomeCfg then 			--不在场景中，请先进入
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.BossHome002, ttFlyTip)
		return
	end

	local nextHomeIdx = bossHomeCfg.homeIdx + 1
	local nextBossHomeCfg = BossHomeConfig[nextHomeIdx]
	if not nextBossHomeCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.BossHome003, ttFlyTip)
		return
	end

	local  oldhomeIdx 	= bossHomeCfg.homeIdx
	local  newhomeIdx 	= nextHomeIdx
	EnterBossHome( sysarg, oldhomeIdx, newhomeIdx )
end

function IsInBossHome( sceneId )
	local bossHomeCfg = GetBossHomeCfgBySceneId( sceneId )
	if bossHomeCfg then
		return true
	end
	return false
end

function QuitBossHome( sysarg )
	local bossHomeCfg = GetBossHomeCfgBySceneId( Actor.getSceneId( sysarg ) )
	if not bossHomeCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.BossHome004, ttFlyTip)
		return
	end

	--BackToMainCity( sysarg )
	Actor.returnCity( sysarg )
end

function EnterBossHomeScene( sysarg, bossHomeCfg )
	--print("EnterBossHomeScene...")
	if not Actor.checkActorLevel( sysarg, bossHomeCfg.enterLevelLimit[2], bossHomeCfg.enterLevelLimit[1] ) then
    	Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016, ttFlyTip)
    	return
    end

	if not Actor.canTelport(sysarg, bossHomeCfg.sceneId) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0031, ttFlyTip)
		return
	end

	if not CheckConsumeCond( sysarg, bossHomeCfg.enterConsume, 1 ) then
		return
	end

	if not DoConsumeCond( sysarg, bossHomeCfg.enterConsume, 1, GameLog.clBossHomeEnterFee, Lang.ScriptTips.BossHomeLog01 ) then
		return
	end

	local enterPos = bossHomeCfg.enterPos
	Actor.enterScene(sysarg, bossHomeCfg.sceneId, enterPos[1], enterPos[2])				--进入场景，不需要右侧面板
	--Actor.closeNPCDialog( sysarg )
end

function GetBossHomeCfgBySceneId( sceneId )
	for homeIdx, HomeCfg in ipairs( BossHomeConfig ) do
		if HomeCfg.sceneId == sceneId then
			return HomeCfg
		end
	end
	return nil
end

--[[
返回值：0-BOSS之家之外，1-BOSS之家首层，2-BOSS之家中间层、3-BOSS之家顶层
扩展槽也要判断
]]
function GetBossHomeSts( sceneId )
	for homeIdx, homeCfg in ipairs( BossHomeConfig ) do
		if homeCfg.sceneId == sceneId then
			return homeCfg.homeLevelSts
		end
	end
	return 0
end

--[[
获取首层场景ID
]]
function GetBossHomeButtomSceneID()
	for homeIdx, homeCfg in ipairs( BossHomeConfig ) do
		if homeCfg.homeIdx then
			return homeCfg.sceneId
		end
	end
	return 0
end

--[[
获取低层场景ID
]]
function GetBossHomeTopSceneID()
	for homeIdx, homeCfg in ipairs( BossHomeConfig ) do
		if homeCfg.isTop then
			return homeCfg.sceneId
		end
	end
	return 0
end


function OnBossHomeBossMob(monster, monId, sceneId, x, y)
	local bossHomeCfg = GetBossHomeCfgBySceneId( sceneId )
	if not bossHomeCfg then
		return
	end

	SetCommonBossKilledTime( monId, 0 )
	BroadBossHomePanelChange( bossHomeCfg, monId )
end

function OnBossHomeBossDeath(monster, monId)
	local sceneId = Actor.getSceneId( monster )
	local bossHomeCfg = GetBossHomeCfgBySceneId( sceneId )
	if not bossHomeCfg then
		return
	end

	local now = System.getCurrMiniTime()
	SetCommonBossKilledTime( monId, now )
	BroadBossHomePanelChange( bossHomeCfg, monId )
end

--[[
初始化右侧面板
]]
function InitBossHomePanel( sysarg )
	local sceneId = Actor.getSceneId( sysarg )
	local bossHomeCfg = GetBossHomeCfgBySceneId( sceneId )
	if not bossHomeCfg then
		return
	end
	local activityId = enCountDownPanelActivity_BossHome
	local panelInfo = {}

	local dDvar = Actor.getDyanmicVar(sysarg)
	local restTime 		= dDvar.enterBossHomeTime + bossHomeCfg.beInTime - System.getCurrMiniTime()
	if restTime < 0 then
		restTime = 0
	end

	panelInfo.title 	= Lang.ScriptTips.BossHomeName			
	--panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	--panelInfo.restTime 	= restTime
	panelInfo.content	= {}
	
	for bossIdx, bossId in ipairs( bossHomeCfg.boss ) do
		local bossCfg = MobBossCommonConfig[bossId]
		if bossCfg then
			local desc 	= System.getMonsterNameById( bossCfg.monsterId )
			local lastTime 	= 0
			local bossNum = System.getMonsterCountById( bossCfg.monsterId, bossCfg.sceneId, 0 )
			if bossNum > 0 then		--BOSS存在
				lastTime = 0
			else
				local timeCd = 0
				local monCfg = MobBossCommonConfig[bossCfg.monsterId]
				if monCfg then
					timeCd = GetBossRevivalCd(bossId, monCfg.revivalCd )
				end
				lastTime = GetCommonBossLastTime( bossCfg.monsterId, timeCd ) --GetMayaPalaceBossLastTime( palaceCfg.palaceIdx, bossCfg )
			end
			panelInfo.content[bossIdx] = { desc, lastTime, bossCfg.monsterId }
		end
	end
	OpenCountDownPanel( sysarg, activityId, panelInfo )
	InitOnlyButtonPanel( sysarg, 2, enRightPanelActivity_BossHome, restTime, Lang.ScriptTips.BossHomeName )
end

--[[
场景广播BOSS状态变化了
]]
function BroadBossHomePanelChange( bossHomeCfg, bossId )
	local bossNum = System.getMonsterCountById( bossId, bossHomeCfg.sceneId, 0 )
	--print("BroadMayaPalaceBossChange, bossNum="..bossNum)
	local playerList = LuaHelp.getSceneActorListById( bossHomeCfg.sceneId ) or {}
	for i,player in ipairs( playerList ) do
		ChangeBossHomeCountDownPanel( player, bossHomeCfg, bossId )
	end
end
function ChangeBossHomeCountDownPanel( sysarg, bossHomeCfg, bossId )
	local content = {}
	local bossCfg 	= MobBossCommonConfig[bossId]
	if bossCfg then
		local desc 		= System.getMonsterNameById( bossCfg.monsterId )
		local lastTime 	= 0
		local bossNum 	= System.getMonsterCountById( bossCfg.monsterId, bossCfg.sceneId, 0 )
		if bossNum > 0 then		--BOSS存在
			lastTime = 0
		else
			local timeCd = 0
			local monCfg = MobBossCommonConfig[bossCfg.monsterId]
			if monCfg then
				timeCd = GetBossRevivalCd(bossId, monCfg.revivalCd )
			end
			lastTime = GetCommonBossLastTime( bossCfg.monsterId, timeCd ) --GetMayaPalaceBossLastTime( palaceCfg.palaceIdx, bossCfg )
		end

		local bossIdx = GetBossIdxByBossId( bossHomeCfg, bossCfg.monsterId )
		if bossIdx > 0 then
			content.idx 	= bossIdx
			content.info	= { desc, lastTime, bossCfg.monsterId }
			--print("ChangeMayaPalaceCountDownPanel, lastTime="..lastTime)
			ChangeCountDownPanel( sysarg, content )
		end
	end
end

--刷出BOSS
--FreshBossHomeBossAll()			--通过FreshBossWhenStart()刷BOSS

ResisterBossHomeProc()				--注册一些事件

